﻿using HslCommunication;
using HslCommunication.Profinet.AllenBradley;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoilIntelStorage.Communication
{
    public class AllenBradleyPlc
    {
        //访问ABPLC的对象
        private readonly AllenBradleyNet allenBradleyNet = new AllenBradleyNet(ConfigurationManager.ConnectionStrings["cranePlc"].ConnectionString);
        public AllenBradleyPlc()
        {
        }
        
        /// <summary>
        /// 写入一个值
        /// </summary>
        /// <param name="address">值的地址</param>
        /// <param name="value">写入的值</param>
        /// <returns>写入结果</returns>
        public bool WriteTagSingleValue<T>(string address, T values)
        {
            OperateResult write = null;
            if (values is float[])
            {
                float[] floatValues = values as float[];
                write = allenBradleyNet.Write(address, floatValues);
        
            }
            else if (values is bool)
            {
                bool boolValue = bool.Parse(values.ToString());
                write = allenBradleyNet.Write(address, boolValue);
            }
            else if (values is byte)
            {
                byte byteValue = byte.Parse(values.ToString());
                write = allenBradleyNet.Write(address, byteValue);
            }
            else if (values is string)
            {
                string stringValue = values.ToString();
                write = allenBradleyNet.Write(address, stringValue);
            }
            else if (values is double[])
            {
                double[] doubleValues = values as double[];
                write = allenBradleyNet.Write(address, doubleValues);
            }
            else if (values is ulong[])
            {
                ulong[] ulongValues = values as ulong[];
                write = allenBradleyNet.Write(address, ulongValues);
            }
            else if (values is uint[])
            {
                uint[] uintValues = values as uint[];
                write = allenBradleyNet.Write(address, uintValues);
            }
            else if (values is long[])
            {
                long[] longValues = values as long[];
                write = allenBradleyNet.Write(address, longValues);
            }
            else if (values is ushort[])
            {
                ushort[] ushortValues = values as ushort[];
                write = allenBradleyNet.Write(address, ushortValues);
            }
            else if (values is short[])
            {
                short[] shortValues = values as short[];
                write = allenBradleyNet.Write(address, shortValues);
            }
            else if (values is byte[])
            {
                byte[] byteValues = values as byte[];
                write = allenBradleyNet.Write(address, byteValues);
            }
            else if (values is int[]) {
                int[] intValues = values as int[];
                write = allenBradleyNet.Write(address, intValues);
            }
            return write.IsSuccess;
        }

    }
}
